<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Base extends Controller
{

    public function before ()
    {
        parent::before ();
		//转换数据,把流数据放到post中
		$this->rowToPost();
		//加载app.php配置文件中plug插件
		!$this->loadPlug() && Public_Helper::response(300);
    }



	/**
	 * 把流数据放到post中
	 */
	public function rowToPost()
	{
		$row = file_get_contents('php://input');
		$post = json_decode($row, true);
		if(!empty($post)){
			foreach($post as $field=>$value){
				$this->request->post($field,$value);
			}
		}
		return true;
	}

	/**
	 * 加载配置文件中的插件
	 */
	public function loadPlug()
	{
		$baseConfig = Kohana::$config->load('base');
		if(!isset($baseConfig['plug']) || !is_array($baseConfig['plug']))
		{
			return true;
		}
		foreach($baseConfig['plug'] as $key=>$className)
		{
			$className = 'Plug_'.$className;
			$obj = $className::instance($this->request);
			if(!($obj instanceof Plug_Base)) {
				Log::instance()->add(Log::WARNING, 'class '.$className.' must implements Plug_Base');
				return false;
			}
			if(! $obj->run() ) {
				return false;
			}
		}
		return true;
	}

}
